//
//  KLMainViewController.m
//  Koala
//
//  Created by cao pan on 11/22/14.
//  Copyright (c) 2014 Anonymous. All rights reserved.
//

#import "KLMainViewController.h"
#import "EXTestData.h"
#import "KLMainTableViewCell.h"
#import "KLMainDataModel.h"
#import "iCarousel.h"
#import "KLBannerView.h"
#import "KLPopView.h"
#import "KLWebViewController.h"
#import "KLAddViewController.h"
#import "KLMyCenterViewController.h"

@interface KLMainViewController ()<UITableViewDelegate,UITableViewDataSource,iCarouselDataSource,iCarouselDelegate,KLPopViewDelegate>
{
    UITableView         * _mainTableView;
    NSMutableArray      * _mainDataArray;
    NSMutableArray      * _bannerDataArray;
    iCarousel           * _carousel;
    UIPageControl       * _pageControl;
    KLPopView           * _popView;
    
}
@end

@implementation KLMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle:@"Home"];
        _mainDataArray  = [[NSMutableArray alloc] init];
        _bannerDataArray = [[NSMutableArray alloc] init];
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addTableView];
    [self getData];
    
    [_bannerDataArray setArray:[KLBanner testBannerArr ]];

    [self setLeftButton:nil normalImage:@"icon_top" highlightImage:nil];
    NSArray * titleArr = @[@"新闻",@"豆壳",@"MIT"];
    [self addNavTitleListView:titleArr];
    [self addNavRightView];
    
    [self addCarousel];
    [self addPageControl];
    [_carousel reloadData];
    [self initPopView];

}


-( void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}


- (void)getData
{
    [self networkRequestStart];
    [KLMainDataModel getMainDataList:^(NSArray *dataArr) {
        [_mainDataArray setArray:dataArr];
        [_mainTableView reloadData];
        [self networkRequestFinish];
    } ];
}

- (void)addTableView
{
    _mainTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [_mainTableView setDelegate:self];
    [_mainTableView setDataSource:self];
    [_mainTableView setOrigin:CGPointMake(0, 170)];
    [_mainTableView setHeight:self.view.height - 200];
    [self.view addSubview:_mainTableView];
}


- (void)addNavRightView
{
    UIView * navRightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightButton setImage:[UIImage imageNamed:@"btn_add_normal"] forState:UIControlStateNormal];
    [rightButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -22)];
    [rightButton addTarget:self action:@selector(clickedAddTagAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightButton1 = [[UIButton alloc] initWithFrame:CGRectMake(35, 0, 44, 44)];
    [rightButton1 setImage:[UIImage imageNamed:@"btn_person"] forState:UIControlStateNormal];
    [rightButton1 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -22)];
    [rightButton1 addTarget:self action:@selector(clickedPersonAction:) forControlEvents:UIControlEventTouchUpInside];

    
    [navRightView addSubview:rightButton];
    [navRightView addSubview:rightButton1];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:navRightView];

}



- (void)clickedAddTagAction:(id)sender
{
    KLAddViewController * addController = [[KLAddViewController alloc] initWithNibName:@"KLAddViewController" bundle:nil];
   [self.navigationController pushViewController:addController animated:YES];
}


- (void)clickedPersonAction:(id)sender
{
    KLMyCenterViewController * centerController = [[KLMyCenterViewController alloc] initWithNibName:@"KLMyCenterViewController" bundle:nil];
    [self.navigationController pushViewController:centerController animated:YES];
}




- (void)addNavTitleListView:(NSArray *)titleArr
{
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(60, 0, 269, 44)];
    for (int i = 0 ; i < 3 ; i++) {
        UILabel * tagLabel = [[UILabel alloc] initWithFrame:CGRectMake( i * (40 + 5), 14, 40, 20)];
        [tagLabel setFont:[UIFont boldSystemFontOfSize:17.0f]];
        [tagLabel setTextColor:[UIColor whiteColor]];
        [tagLabel setText:[titleArr objectAtIndex:i]];
        [view addSubview:tagLabel];
    }
    
    if (titleArr.count >= 3) {
        UIButton * guideButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [guideButton setImage:[UIImage imageNamed:@"icon_down_normal"] forState:UIControlStateNormal];
        [guideButton setImage:[UIImage imageNamed:@"icon_down_press"] forState:UIControlStateHighlighted];
        [guideButton setImage:[UIImage imageNamed:@"icon_up_normal"] forState:UIControlStateSelected];
        [guideButton setFrame:CGRectMake(120, 0, 40, 44)];
        [guideButton addTarget:self action:@selector(clickedGuideButton:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:guideButton];
    }
    
    [self.navigationItem setTitleView:view];
    
}



static BOOL gIsSelected;

- (void)clickedGuideButton:(id)sender
{
    
    gIsSelected = ! gIsSelected;
    
    UIButton * button = (UIButton *)sender;
    [button setSelected:gIsSelected];
 

    if (gIsSelected) {
        [_popView show];
        
    }else{
        [_popView dismiss];
    }
    
}

- (void)initPopView
{
    _popView = [[KLPopView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    [self.view addSubview:_popView];
    [_popView setDelegate:self];
    [_popView setAlpha:0];
}

- (void)didSelectedPopIndex:(NSUInteger)index
{
    [_popView dismiss];
    gIsSelected = YES;
}


- (void)addCarousel
{
    _carousel = [[iCarousel alloc] init];
    [_carousel setFrame:CGRectMake(0, 0, 320, 150)];
    [_carousel setDelegate:self];
    [_carousel setBackgroundColor:[UIColor clearColor]];
    [_carousel setType:iCarouselTypeLinear];
    [_carousel setDelegate:self];
    [_carousel setDataSource:self];
    [self.view addSubview:_carousel];
}


- (void)addPageControl
{
     _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(120, 140, 100, 20)];
    [_pageControl setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.4]];
    [_pageControl setNumberOfPages:[_bannerDataArray count]];
    [_pageControl.layer setCornerRadius:5];
    [self.view addSubview:_pageControl];
}


#pragma mark -------- iCarousel methods

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [_bannerDataArray count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    KLBannerView * bannerView = [[KLBannerView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
    [bannerView setContentMode:UIViewContentModeScaleAspectFill];
    [bannerView setClipsToBounds:YES];
    KLBanner * banner = [_bannerDataArray objectAtIndex:index];
    [bannerView updateByBanner:banner];
    return bannerView;
}



- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
    [_pageControl setCurrentPage:carousel.currentItemIndex];
}



- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            return YES;
        }
        case iCarouselOptionSpacing:
        {
            return value * 1.05f;
        }
        case iCarouselOptionFadeMax:
        {
            if (carousel.type == iCarouselTypeCustom)
            {
                return 0.0f;
            }
            return value;
        }
        default:
        {
            return value;
        }
    }
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return  [_mainDataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * sTableViewIndentifier = @"KLMainTableViewCell";
    KLMainTableViewCell * tableViewCell = [tableView dequeueReusableCellWithIdentifier:sTableViewIndentifier];
   
    if (tableViewCell == nil) {

      tableViewCell = [[KLMainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sTableViewIndentifier];
    }
    
    [[[tableViewCell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview) withObject:nil];

    KLMainDataModel * dataModel = [_mainDataArray objectAtIndex:indexPath.row];
    [tableViewCell updateByMainModel:dataModel];
    
    return tableViewCell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [KLMainTableViewCell heightOfMainTableViewCell];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KLWebViewController * webController = [[KLWebViewController alloc] initWithNibName:@"KLWebViewController" bundle:nil];
    KLMainDataModel * dataModel = [_mainDataArray objectAtIndex:indexPath.row];
    [webController setMainDataModel:dataModel];
    [self.navigationController pushViewController:webController animated:YES];
}




@end
